import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { list as productList } from '@/views/mds/product/Product.api';
// import { rules } from '/@/utils/helper/validator';
// import { render } from '/@/utils/common/renderUtils';
import { ref } from 'vue';
import { trim } from 'lodash-es';
import { isOrgExist } from '/@/views/mds/dealer/Org.api';

export const columns: BasicColumn[] = [
  {
    title: '经销商ID',
    dataIndex: 'ownerAscId',
  },
  {
    title: '经销商名称',
    dataIndex: 'ownerAscName',
  },
  {
    title: '产品ID',
    dataIndex: 'productAscId',
  },
  {
    title: '产品名称',
    dataIndex: 'productAscName',
  },
  {
    title: '产品原始编码',
    dataIndex: 'code',
  },
  {
    title: '产品原始别称',
    dataIndex: 'commonname',
  },
  {
    title: '单位',
    dataIndex: 'unit',
  },
  {
    title: '规格',
    dataIndex: 'spec',
  },
  {
    title: '生产厂商',
    dataIndex: 'origin',
  },
  {
    title: '换算比',
    dataIndex: 'discount',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    label: '经销商名称',
    field: 'ownerAscName',
    component: 'Input',
  },
  {
    label: '产品名称',
    field: 'productAscName',
    component: 'Input',
  },
  {
    label: '产品原始别称',
    field: 'commonname',
    component: 'Input',
  },
  {
    label: '产品原始编码',
    field: 'code',
    component: 'Input',
  },
];
const commonname = ref<string>('');
export const formSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  { label: '', field: 'id', component: 'Input', show: false },
  {
    label: '经销商名称',
    field: 'ownerAscName',
    component: 'Input',
    rules: [
      {
        required: true,
        message: '请输入经销商名称',
      },
      {
        validator(_, value) {
          return new Promise((resolve, reject) => {
            isOrgExist(value)
              .then(() => resolve())
              .catch((err) => {
                reject(err.message || '验证失败');
              });
          });
        },
      },
    ],
  },
  { label: '', field: 'productAscId', component: 'Input', show: false },
  {
    label: '产品名称',
    field: 'productAscName',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formModel }) => {
      return {
        api: productList,
        params: {
          commonname: commonname.value,
        },
        filterOption: false,
        showSearch: true,
        resultField: 'records',
        labelField: 'commonname',
        valueField: 'id',
        immediate: false,
        onSearch: function (value) {
          commonname.value = `*${trim(value)}*`;
        },
        onChange: function (value, e) {
          console.log(e);
          formModel.productAscId = value;
          formModel.commonname = e.label;
        },
      };
    },
  },
  {
    label: '产品原始编码',
    field: 'code',
    component: 'Input',
    required: true,
  },
  {
    label: '产品原始别称',
    field: 'commonname',
    component: 'Input',
  },
  {
    label: '单位',
    field: 'unit',
    component: 'Input',
  },
  {
    label: '规格',
    field: 'spec',
    component: 'Input',
  },
  {
    label: '生产厂商',
    field: 'origin',
    component: 'Input',
  },
  {
    label: '换算比',
    field: 'discount',
    component: 'Input',
  },
];
